import { axiosInstance } from 'src/boot/axios'
import axios from 'axios'
import { loadToken } from 'yz-help'
import qs from 'qs'
import CommonConfig from '../../assets/js/config'

const baseUrl = '/v1' + '/oauth_clients'
const messageUrl = CommonConfig.messagestationUrl + '/v1'
// const opendata = 'opendata/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
/**
 * 创建客户端类型的OAuth Client
 */
export function createClientCredentials () {
  const newUrl = baseUrl + '/client_credentials'

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取客户端类型的OAuth Client详情信息
 */
export function getClientCredentials () {
  const newUrl = process.env.API_BASE_URL + 'v1/oauth_clients/client_credentials'

  return axios.get(newUrl, { headers: { 'Authorization': `bearer ${loadToken()}` } }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getDataPushConfigs () {
  const newUrl = 'opendata/v1/data_push_configs'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function createSimpleAuthentications () {
  const newUrl = 'opendata/v1/simple_authentications'

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function getSimpleAuthentications () {
  const newUrl = 'opendata/v1/simple_authentications'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function createDataPush (data) {
  const newUrl = 'opendata/v1/data_push_items'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

export function getDataPush (data) {
  const newUrl = 'opendata/v1/data_push_items'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function updateDataPush (id, data) {
  const newUrl = `opendata/v1/data_push_items/${id}`

  return axiosInstance.put(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

export function deleteDataPush (id, pushType) {
  const newUrl = `opendata/v1/data_push_items/${id}?pushType=${pushType}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function resetDataPush (id, pushType) {
  const newUrl = `opendata/v1/data_push_items/${id}:reset`

  return axiosInstance.post(newUrl, pushType).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 编辑推送邮件人员
 * @param msgGroupId
 */
export function bizTemplatesEmail (msgGroupId) {
  const newUrl = `${messageUrl}/message/biz_templates/email`

  const config = {
    params: {
      msgGroupId
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.put(newUrl, null, config).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 列举邮箱推送role
 * @param msgGroupId
 */
export function getTemplatesEmail () {
  const newUrl = `${messageUrl}/message/biz_templates/email/roles`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
